Git Repository Size Optimization: Techniques for Cleaning Large Files and History
The main reasons for a growing Git repository are committing large files (e.g., logs, videos), leftover large files in historical records, and unoptimized submodules. This leads to slow cloning/downloads, time-consuming backup transfers, and local operation lag. Cleanup methods: For recently committed but un-pushed large files, use `git rm --cached` to remove cached files, then re-commit and push. For large files in historical records, rewrite history with `git filter-repo` (install the tool, filter large files, and force push updates). After cleanup, verify with `git rev-list` to check for omissions. Ultimate solution: Batch cleanup can use `--path-glob` to match files. Large submodule files require prior cleanup before updating. Long-term optimization recommends Git LFS for managing large files (track large file types after installation to avoid direct commits). Always back up the repository before operations. Use force pushes cautiously in collaborative environments; ensure team confirmation before execution. Develop the habit of committing small files and using LFS for large files to keep the repository streamlined long-term.
Read MoreGit Repository Cleanup: Methods to Delete Unused Local and Remote Branches
The article introduces the necessity, steps, and precautions for cleaning up useless Git branches. Necessity: reducing repository clutter, lowering the risk of accidental deletion, and saving storage space. Before cleaning, confirm permissions, check branch status (whether merged), and back up important branches. For local deletion: First, list branches with `git branch --merged main_branch` to filter merged branches. After confirmation, delete merged branches with `git branch -d branch_name`, and use `-D` for unmerged branches (high risk). For remote deletion: Directly delete remote branches with `git push origin --delete branch_name`, or use `git fetch -p` to clean up locally tracked remote obsolete branches. Advanced tips: Batch delete merged branches locally using `git branch --merged master | grep -v '^\*\|master\|main' | xargs git branch -d`, and similar looping commands for remote branches. Precautions: Confirm if branches are in use by others, avoid deleting unmerged branches by mistake, and note that recovery is difficult after deletion. When regularly cleaning, confirm status first to ensure safety and efficiency.
Read More